package com.cms.bs.config;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by jiang on 2016-11-14.
 */
public class SessionHelper {

    private static HttpServletRequest request;
    private static HttpSession session;

    static {
        request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static Object get(String key) {
        session = request.getSession();
        if(session!=null){
            return session.getAttribute(key);
        }
        return null;
    }

    public static void set(String key, Object object) {
        session = request.getSession();
        if(session==null){
            session = request.getSession(true);
        }
        session.setAttribute(key, object);
    }

}
